package com.cykj.page04;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page04
 * @Author: 阿隆
 * @CreateTime: 2025-11-15 09:54
 * @Description: TODO
 * @Version: 1.0
 */
public class Lesson01 {
    public static void main(String[] args) {
        /*
        *
        * public ThreadPoolExecutor(
        *                 int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler)
                          *
            corePoolSize - 即使空闲时仍保留在池中的线程数，除非设置 allowCoreThreadTimeOut
            maximumPoolSize - 池中允许的最大线程数
            keepAliveTime - 当线程数大于内核时，这是多余的空闲线程在终止前等待新任务的最大时间。
            unit - keepAliveTime参数的时间单位
            workQueue - 用于在执行任务之前使用的队列。 这个队列将仅保存execute方法提交的Runnable任务。
            threadFactory - 执行程序创建新线程时使用的工厂
            handler - 执行被阻止时使用的处理程序，因为达到线程限制和队列容量

     ------------------------------------------------------------------------------------
//            corePoolSize:    核心线程数
           maximumPoolSize:    最大线程数
             keepAliveTime:     空闲时间
                      unit:     时间单位
                 workQueue:     工作队列(线程任务)
             threadFactory:     线程工厂
                   handler:     拒绝策略

                银行大厅
        * */
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                3,  // 最大线程 >= 核心线程
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        // 线程池中的临时线程 什么时候会执行?
        /*
        * 最大线程数 跟 线程队列满了  就会开启临时线程
        * */

        MyRunnable myRunnable1 = new MyRunnable();
        MyRunnable myRunnable2 = new MyRunnable();
        MyRunnable myRunnable3 = new MyRunnable();
        MyRunnable myRunnable4 = new MyRunnable();
        MyRunnable myRunnable5 = new MyRunnable();
        MyRunnable myRunnable6 = new MyRunnable();
        MyRunnable myRunnable7 = new MyRunnable();

        threadPoolExecutor.execute(myRunnable1);
        threadPoolExecutor.execute(myRunnable2);
        threadPoolExecutor.execute(myRunnable3);
        threadPoolExecutor.execute(myRunnable4);
        threadPoolExecutor.execute(myRunnable5);
        threadPoolExecutor.execute(myRunnable6);
        threadPoolExecutor.execute(myRunnable7);


    }
}